# HTTrack Website Copier, Offline Browser for Windows and Unix
# Copyright (C) Xavier Roche and other contributors
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# Version: 1.3 (09/2001)
# Usage: just type 'make'

# Generated by configure
AUTONAME  = 
#__AUTONAME__


### Makefile commands
SHELL     = /bin/sh
MAIN      =
CC        = gcc 
CFLAGS    = __CFLAGS__
# threads
LFLAGS    = __LFLAGS__ __LFLAGS2__ __LFLAGS3__


BINARIES  = htscore.o htsback.o htscache.o\
	htscatchurl.o htsfilters.o htsftp.o htshash.o\
	htshelp.o htsjava.o htslib.o htscoremain.o\
	htsname.o htsrobots.o htstools.o htswizard.o\
	htsalias.o htsthread.o htsindex.o\
	htsbauth.o htsmd5.o\
	htswrap.o md5.o htszlib.o\
	htsnostatic.o \
	httrack.o

SOBINARIES = htscore.o htsback.o htscache.o\
	htscatchurl.o htsfilters.o htsftp.o htshash.o\
	htshelp.o htsjava.o htslib.o htscoremain.o\
	htsname.o htsrobots.o htstools.o htswizard.o\
	htsalias.o htsthread.o htsindex.o\
	htsbauth.o htsmd5.o htszlib.o\
	htsnostatic.o \
	htswrap.o md5.o

MAINBIN = httrack.c

BOUTPUT     = httrack
BOUTPUTSO   = libhttrack.so
DOCS      = ../HelpHtml ../httrack ../httrack-doc.html ../COPYING ../INSTALL ../README ../*.txt
HTSSYSTEM = htssystem.h
BINPATH   = __BINPATH__
ETCPATH   = __ETCPATH__
LIBPATH   = __LIBPATH__
PREFIX    = __PREFIX__

## Defines for "library" (program compiled with HTTrack)
BINARIES_LIB = example.o httracklib.o\
	src/htscore.o src/htsback.o src/htscache.o\
	src/htscatchurl.o src/htsfilters.o src/htsftp.o src/htshash.o\
	src/htshelp.o src/htsjava.o src/htslib.o src/htscoremain.o\
	src/htsname.o src/htsrobots.o src/htstools.o src/htswizard.o\
	src/htsalias.o src/htsthread.o src/htsindex.o\
	src/htsbauth.o src/htsmd5.o htszlib.o\
	src/htsshow.o src/htswrap.o\
	src/htsnostatic.o \
	src/md5.o
BOUTPUT_LIB = example
HTSSYSTEM_LIB = src/htssystem.h

# in_addr_t problems :
# In case of problems during compiling,
# make htss
# edit htssystem.h and add the following line:
# #define HTS_DO_NOT_REDEFINE_in_addr_t
# make manual


# Keywords for build types (example: make linux)
MAKE_LINUX   = linux
MAKE_NETBSD  = netbsd
MAKE_OPENBSD = openbsd
MAKE_SOLARIS = solaris
MAKE_AIX     = aix
MAKE_STD     = standard
MAKE_STD2    = standard2
MAKE_STD3    = standard3
MAKE_STD4    = standard4
###
MAKE_LIB     = build_httracklib
###
MAKE_AUTO    = auto 
###

# First, detect OS Type
# If your make does not recognize this, change it!
SHORTUNAME = $(shell uname)
FULLUNAME = $(shell uname -a)

### Targets:

# Default target, attempt to use uname if necessary
# if uname is empty, display info message
all :
	@if test -n "$(AUTONAME)"; then\
		$(MAKE) $(AUTONAME);\
	elif test -n "$(SHORTUNAME)"; then\
		$(MAKE) $(SHORTUNAME);\
	else\
		$(MAKE) help;\
	fi

# If we can not detect the OS Type, show a message info
help :
	@clear
	@echo ""
	@echo "Welcome to HTTrack Website Copier install!"
	@echo "-----------------------------------------"
	@echo ""
	@echo "1. To make HTTrack, just type in:"
	@echo "    make $(MAKE_LINUX)"
	@echo "  or"
	@echo "    make $(MAKE_NETBSD)"
	@echo "  or"
	@echo "    make $(MAKE_OPENBSD)"
	@echo "  or"
	@echo "    make $(MAKE_SOLARIS)"
	@echo "  or"
	@echo "    make $(MAKE_AIX)"
	@echo "  or"
	@echo "    make $(MAKE_STD)"
	@echo "  or (problems with in_addr_t)"
	@echo "    make $(MAKE_STD2)"
	@echo "  or (problems with 64-bit)"
	@echo "    make $(MAKE_STD3)"
	@echo "  or (problems with both in_addr_t and 64-bit)"
	@echo "    make $(MAKE_STD4)"
	@echo ""
	@echo "According to your OS type"
	@echo "(example: type in 'make $(MAKE_LINUX)' if you compile HTTrack with linux)"
	@echo 
	@echo "Or, if it does not work, you can try "
	@echo "  make htss"
	@echo "  edit htssystem.h (check OS type), and add the following line:"
	@echo "    #define HTS_DO_NOT_REDEFINE_in_addr_t"
	@echo "  make manual"
	@echo 
	@echo "2. Then, type in 'make install' to copy httrack to $(BINPATH)"
	@echo "   or just use ./httrack to launch the program"
	@echo ""
	@echo "3. Build problems, type in:"
	@echo "   make moreinfo"
	@echo ""
	@echo "Have fun with HTTrack Website Copier!"
	@echo ""
info : help

# Troubleshooter
moreinfo :
	@echo "Known problems:"
	@echo ""
	@echo "\`in_addr_t' undeclared (first use this function)"
	@echo "see in_addr_t problems in Makefile"
	@echo ""

###

## Build Targets (this is the name given by 'uname')
Linux   : $(MAKE_LINUX)
SunOS   : $(MAKE_SOLARIS)
AIX     : $(MAKE_AIX)
NetBSD  : $(MAKE_NETBSD)
OpenBSD : $(MAKE_OPENBSD)

### Build Targets (standard types)
default           : firstinfo htssystem htssystem_default build_default strip clean lastinfo
$(MAKE_LINUX)     : firstinfo htssystem htssystem_linux   build_default strip clean lastinfo
$(MAKE_SOLARIS)   : firstinfo htssystem htssystem_solaris build_solaris strip clean lastinfo
$(MAKE_AIX)       : firstinfo htssystem htssystem_aix     build_default strip clean lastinfo
$(MAKE_NETBSD)    : firstinfo htssystem htssystem_netbsd  build_default strip clean lastinfo
$(MAKE_OPENBSD)   : firstinfo htssystem htssystem_openbsd build_nopthread strip clean lastinfo
$(MAKE_STD)       : firstinfo htssystem htssystem_default build_default strip clean lastinfo
$(MAKE_STD2)      : firstinfo htssystem htssystem_default2 build_default strip clean lastinfo
$(MAKE_STD3)      : firstinfo htssystem htssystem_default3 build_default strip clean lastinfo
$(MAKE_STD4)      : firstinfo htssystem htssystem_default4 build_default strip clean lastinfo
### Defines for "library" (program compiled with HTTrack)
$(MAKE_LIB)       : build_lib strip_lib clean_lib lastinfo
###
$(MAKE_AUTO)      : __MAKEAUTO__
###

## Defines for OSes
lib_default   : htssystem htssystem_default addhtssystem_lib info_lib
lib_linux     : htssystem htssystem_linux   addhtssystem_lib info_lib
lib_solaris   : htssystem htssystem_solaris addhtssystem_lib info_lib
lib_aix       : htssystem htssystem_aix     addhtssystem_lib info_lib
lib_netbsd    : htssystem htssystem_netbsd  addhtssystem_lib info_lib
lib_openbsd   : htssystem htssystem_openbsd addhtssystem_lib info_lib
lib_std       : htssystem htssystem_default addhtssystem_lib info_lib


# manual build
htss             : htssystem htssystem_default
manual           : build_default strip clean lastinfo

# Creates htssystem.h file
htssystem :
	@echo "/* HTTrack, Offline Browser for Windows and Unix */"    > $(HTSSYSTEM)
	@echo ""                                                      >> $(HTSSYSTEM)
	@echo "/* HTTrack system definition */"                       >> $(HTSSYSTEM)
	@echo "/* This should be the only file you have to change */" >> $(HTSSYSTEM)
	@echo ""                                                      >> $(HTSSYSTEM)
	@echo "/* Solaris: 0 / Windows: 1 / AIX: 2 / Linux: 3 */"     >> $(HTSSYSTEM)
	@echo ""                                                      >> $(HTSSYSTEM)
	@echo ""                                                      >> $(HTSSYSTEM)
	@echo "/* Fix plateform number to 0 (SunOS) */"               >> $(HTSSYSTEM)
	@echo "/* If it doesn't compile, try another one */"          >> $(HTSSYSTEM)

htssystem_solaris :
	@echo "#define HTS_PLATFORM 0"                                >> $(HTSSYSTEM)
	@echo "#define HTS_PLATFORM_NAME \"$(FULLUNAME)\""            >> $(HTSSYSTEM)
	@echo "#define HTS_LITTLE_ENDIAN"                             >> $(HTSSYSTEM)
	@echo ""                                                      >> $(HTSSYSTEM)

htssystem_aix :
	@echo "#define HTS_PLATFORM 2"                                >> $(HTSSYSTEM)
	@echo "#define HTS_PLATFORM_NAME \"$(FULLUNAME)\""            >> $(HTSSYSTEM)
	@echo "#define HTS_LITTLE_ENDIAN"                             >> $(HTSSYSTEM)
	@echo ""                                                      >> $(HTSSYSTEM)

htssystem_linux :
	@echo "#define HTS_PLATFORM 3"                                >> $(HTSSYSTEM)
	@echo "#define HTS_PLATFORM_NAME \"$(FULLUNAME)\""            >> $(HTSSYSTEM)
	@echo ""                                                      >> $(HTSSYSTEM)

htssystem_netbsd:
	@echo "#define HTS_PLATFORM 3"                                >> $(HTSSYSTEM)
	@echo "#define HTS_PLATFORM_NAME \"$(FULLUNAME)\""            >> $(HTSSYSTEM)
	@echo "#define HTS_DO_NOT_REDEFINE_in_addr_t"                 >> $(HTSSYSTEM)
	@echo ""                                                      >> $(HTSSYSTEM)

htssystem_openbsd:
	@echo "#define HTS_PLATFORM 3"                                >> $(HTSSYSTEM)
	@echo "#define HTS_PLATFORM_NAME \"$(FULLUNAME)\""            >> $(HTSSYSTEM)
	@echo "#define HTS_DO_NOT_REDEFINE_in_addr_t"                 >> $(HTSSYSTEM)
	@echo "#define HTS_DO_NOT_USE_PTHREAD"                        >> $(HTSSYSTEM)
	@echo "#define HTS_DO_NOT_USE_UID"                            >> $(HTSSYSTEM)
	@echo ""                                                      >> $(HTSSYSTEM)

htssystem_nopthread:
	@echo "#define HTS_PLATFORM 3"                                >> $(HTSSYSTEM)
	@echo "#define HTS_PLATFORM_NAME \"$(FULLUNAME)\""            >> $(HTSSYSTEM)
	@echo "#define HTS_DO_NOT_USE_PTHREAD"                        >> $(HTSSYSTEM)
	@echo ""                                                      >> $(HTSSYSTEM)

htssystem_default :
	@echo "#define HTS_PLATFORM 3"                                >> $(HTSSYSTEM)
	@echo "#define HTS_PLATFORM_NAME \"$(FULLUNAME)\""            >> $(HTSSYSTEM)
	@echo ""                                                      >> $(HTSSYSTEM)

htssystem_default2 :
	@echo "#define HTS_PLATFORM 3"                                >> $(HTSSYSTEM)
	@echo "#define HTS_PLATFORM_NAME \"$(FULLUNAME)\""            >> $(HTSSYSTEM)
	@echo "#define HTS_DO_NOT_REDEFINE_in_addr_t"                 >> $(HTSSYSTEM)
	@echo ""                                                      >> $(HTSSYSTEM)

htssystem_default3 :
	@echo "#define HTS_PLATFORM 3"                                >> $(HTSSYSTEM)
	@echo "#define HTS_PLATFORM_NAME \"$(FULLUNAME)\""            >> $(HTSSYSTEM)
	@echo "#define HTS_NO_64_BIT"                                 >> $(HTSSYSTEM)
	@echo ""                                                      >> $(HTSSYSTEM)

htssystem_default4 :
	@echo "#define HTS_PLATFORM 3"                                >> $(HTSSYSTEM)
	@echo "#define HTS_PLATFORM_NAME \"$(FULLUNAME)\""            >> $(HTSSYSTEM)
	@echo "#define HTS_DO_NOT_REDEFINE_in_addr_t"                 >> $(HTSSYSTEM)
	@echo "#define HTS_NO_64_BIT"                                 >> $(HTSSYSTEM)
	@echo ""                                                      >> $(HTSSYSTEM)

# Generated by configure
htssystem_auto :
	@echo "#define HTS_PLATFORM __PLATFORM__"                      >> $(HTSSYSTEM)
	@echo "#define HTS_PLATFORM_NAME \"$(FULLUNAME)\""             >> $(HTSSYSTEM)
	@echo "#define __DEFINEUID__"                                  >> $(HTSSYSTEM)
	@echo "#define __DEFINEINA__"                                  >> $(HTSSYSTEM)
	@echo "#define __DEFINEPTH__"                                  >> $(HTSSYSTEM)
	@echo "#define __DEFINE64B__"                                  >> $(HTSSYSTEM)
	@echo "#define __DEFINEFTI__"                                  >> $(HTSSYSTEM)
	@echo "#define HTS_PREFIX \"__DEFINEPRE__\""                   >> $(HTSSYSTEM)
	@echo "#define HTS_BINPATH \"__BINPATH__\""                    >> $(HTSSYSTEM)
	@echo "#define HTS_ETCPATH \"__ETCPATH__\""                    >> $(HTSSYSTEM)
	@echo "#define HTS_LIBPATH \"__LIBPATH__\""                    >> $(HTSSYSTEM)
	@echo "#define HTS_USEZLIB __ZLIB__"                           >> $(HTSSYSTEM)
	@echo "#define HTS_ALIGN __PTRALIGN__"                         >> $(HTSSYSTEM)
	@echo "#define HTS_INET6 __INET6__"                            >> $(HTSSYSTEM)
	@echo "#define HTS_USEOPENSSL __SSL__"                         >> $(HTSSYSTEM)

## Defines for "library" (program compiled with HTTrack)
addhtssystem_lib :
	@echo "/* Extended functions */"                              >> $(HTSSYSTEM)
	@echo "#define HTS_ANALYSTE 2"                                >> $(HTSSYSTEM)
	@echo ""                                                      >> $(HTSSYSTEM)

# Info message before build
firstinfo :
	@echo "Building all, please wait"
	@echo "In case of problems, type in:"
	@echo "make help"
	@echo ""
	@echo "OS TYPE:   $(SHORTUNAME)"
	@echo "Make mode: $(MAKECMDGOALS)"
	@echo

##
info_lib : 
	@echo "Please copy htssystem.h to src/htssystem.h by typing:"
	@echo "cp htssystem.h src/htssystem.h"

### Targets for compiling
build_solaris : $(BINARIES)
	$(CC) $(CFLAGS) $(BINARIES) -o $(BOUTPUT) $(LFLAGS) -lnsl -lsocket
	chmod a+rx $(BOUTPUT)

build_nopthread: $(BINARIES)
	$(CC) $(NOPCFLAGS) $(BINARIES) -o $(BOUTPUT) $(NOPLFLAGS)
	chmod a+rx $(BOUTPUT)

build_default : $(BINARIES)
	$(CC) $(CFLAGS) $(BINARIES) -o $(BOUTPUT) $(LFLAGS)
	chmod a+rx $(BOUTPUT)

## Defines for "library" (program compiled with HTTrack)
build_lib : $(BINARIES_LIB)
	$(CC) $(CFLAGS) $(BINARIES_LIB) -o $(BOUTPUT_LIB) $(LFLAGS)
	chmod a+rx $(BOUTPUT_LIB)

## Auto
build_auto : build_bin__DYNAMIC__

build_bin : $(BINARIES)
	$(CC) $(CFLAGS) $(BINARIES) -o $(BOUTPUT) $(LFLAGS)
	chmod a+rx $(BOUTPUT)

build_binso : $(SOBINARIES)
	$(CC) $(CFLAGS) -shared -Wl,-x,-soname,$(BOUTPUTSO) -o $(BOUTPUTSO) $(SOBINARIES) -lc $(LFLAGS)
	$(CC) -L. -lhttrack $(MAINBIN) -o $(BOUTPUT)
	chmod a+rx $(BOUTPUT)

##
# Strip file so that is can be shorter
strip :
	strip $(BOUTPUT)

strip_lib :
	strip --strip-unneeded $(BOUTPUT_LIB)

# Cleaning up..
clean :
	rm -f $(BINARIES)

## Defines for "library" (program compiled with HTTrack)
clean_lib :
	rm -f $(BINARIES_LIB)

# Bye bye
lastinfo :
	@echo "Build successful"

# Installing httrack into the correct folder
install : __INSTALL__
uninstall : remove
remove : __UNINSTALL__

# Install docs
docinstall :
	mkdir -p "$(PREFIX)/doc/httrack" && chmod a+rx "$(PREFIX)/doc/httrack"
	cp -fR $(DOCS) "$(PREFIX)/doc/httrack/"

# Uninstall docs
docremove :
	rm -rf "$(PREFIX)/doc/httrack"

# Install binaries and conf files
bininstall :
	@echo "Copying $(BOUTPUT) to $(BINPATH).."
	test ! -d "$(BINPATH)" && (mkdir -p "$(BINPATH)" && chmod a+rx "$(BINPATH)") || true
	cp -f $(BOUTPUT) $(BINPATH)
	chmod a+rx $(BINPATH)/$(BOUTPUT)

libremove :
	rm -f "$(LIBPATH)/$(BOUTPUTSO)"

libinstall :
	@echo "Copying $(BOUTPUTSO) to $(LIBPATH)/.."
	test ! -d  "$(LIBPATH)" && (mkdir -p "$(LIBPATH)" && chmod a+rx "$(LIBPATH)") || true
	cp -f $(BOUTPUTSO) $(LIBPATH)/
	chmod a+rx $(LIBPATH)/$(BOUTPUTSO)
	ln -sf "$(BOUTPUTSO)" "$(PREFIX)/lib/$(BOUTPUTSO).1"


# Uninstall binaries
binremove :
	rm -f $(BINPATH)/$(BOUTPUT)
	rm -f $(ETCPATH)/httrack.conf

# Configure program
config :
	@./postinst-config

###

